package com.sq.partner.manager.service.impl;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.dao.ICoreDeptDao;
import com.sq.partner.manager.dao.IGatewayChannelInfoConfigDao;
import com.sq.partner.manager.dao.IGatewayCustomerDao;
import com.sq.partner.manager.dao.IGatewayEmailDao;
import com.sq.partner.manager.dao.IGatewayPolicyDao;
import com.sq.partner.manager.dao.IGatewaySMSDao;
import com.sq.partner.manager.dao.IGatewaySubChannelDao;
import com.sq.partner.manager.mybatis.model.CoreDept;
import com.sq.partner.manager.mybatis.model.GatewayChannelInfo;
import com.sq.partner.manager.mybatis.model.GatewayEmail;
import com.sq.partner.manager.mybatis.model.GatewayPolicy;
import com.sq.partner.manager.mybatis.model.GatewaySMS;
import com.sq.partner.manager.mybatis.model.TreeDto;
import com.sq.partner.manager.service.ScanPolicyListService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.excel.ExcelEntity;
import com.sq.partner.manager.util.excel.ExportExcelBuilderTool;
@Service
public class ScanPolicyListServiceImpl implements ScanPolicyListService {

	@Resource
	private ICoreDeptDao coreDeptDao;
	
	@Resource
	private IGatewayChannelInfoConfigDao channelInfoConfigDao;
	
	@Resource
	private IGatewaySubChannelDao subChannelConfigDao;
	
	@Resource
	private IGatewayPolicyDao policyDao;
	
	@Resource
	ExportExcelBuilderTool exportExcelBuilderTool;
	@Resource
	private IGatewayCustomerDao customerDao;
	
	 @Resource
	 private IGatewayEmailDao gatewayEmailDao;
	 @Resource
	 private IGatewaySMSDao gatewaySMSDao;
	
	@Override
	public Page<GatewayChannelInfo> queryChannelInfoPageOfFacadeNlife(
			Page<GatewayChannelInfo> page) throws Exception {
		return channelInfoConfigDao.queryChannelInfoPageOfFacadeNlife(page);
	}
	@Override
	public Page queryScanPolicyPageByCriteria(
			Page page) throws Exception {
		
		return policyDao.queryScanPolicyPageByCriteria(page);
	}

	@Override
	public List<Map<String, Object>> queryScanPolicyListByCriteria(
			Map<String, Object> map) throws Exception {
		
		return policyDao.queryScanPolicyListByCriteria(map);
	}
	@Override
	public GatewayPolicy queryScanPolicyInfoByPolNo(String polNo) throws Exception {
		return policyDao.queryScanPolicyInfoByPolNo(polNo);
	}

	/**
	 * 查询总量
	 */
	@Override
	public Map<String, String> queryLifePolicyAll(Map<String, String> map) throws Exception { 
	    return policyDao.queryScanPolicyAll(map);
	}
	/**
     * 导出Excel
     */
    @Override
    public void downloadLifePolicyFile(List<Map<String, Object>> lifePolicyList,HttpServletResponse response)
            throws Exception {
        ExcelEntity excel = new ExcelEntity();
        excel.setResults(lifePolicyList);
        excel.setFileName("ScanPolicyInfo");
        excel.setTitles("保单号,合作伙伴名称,投保方案名称,用户名（个人）,姓名,被保险人,被保人证号,含税保费,保单提交日期,起保日期,终保日期,学校名称,年级,班级,车牌号,车架号,被保人邮箱,状态,手续费比例,医院名称,科室名称,手术类型,医生姓名");
        // excel.setTitleKeys("POLNO,TBR,CHANNEL_NAME,SUB_CHANNEL_NAME,PARTNER_SUB_CODE,INSU_PLAN_NAME,BBR_NUM,PREMIUM,INSERT_TIME,BEGIN_DATE,END_DATE,CARD_NO,STATE_ID,APP_CONTENT,PURVEYOR_NAME,PURVEYOR_TEL,USER_CODE,USER_NAME,THIRD_AGENT");
		excel.setTitleKeys("POL_NO,CHANNEL_NAME,PLAN_NAME,USER_CODE,USER_NAME,CLIENT_NAME,ID_NO,PREMIUM,INSERT_TIME,BEGIN_DATE,END_DATE,SCHOOLNAME,GRAD,CLASSNAME,PLATE_NUM,CHASSIS_NUM,EMAIL,STATUS,COMM_RATE,HOSPITAL_NAME,DEPARTMENT,OPERTYPE,DOCTOR_NAME");
		excel.setColumns("35,35,35,20,20,10,25,15,25,25,25,30,15,15,15,10,20,10,25,25,25,10,15");
        exportExcelBuilderTool.buildExcel(response,excel);      
    }
       
    @Override
	public Result addEmailReSend(String polNo, String email) throws Exception {
		/**查询已发送过的邮件*/
		List<GatewayEmail> emails=gatewayEmailDao.queryEmailByPolNo(polNo);
		if(emails==null||emails.size()==0){
			return Result.error("此单没有历史邮件发送记录");
		}
		GatewayEmail emailDto=emails.get(0);
		emailDto.setEmail(email);
		if(emailDto.getEmailId().contains("CF")){
			emailDto.setEmailId("CF"+(Integer.parseInt(emailDto.getEmailId().replace("CF", ""))+emails.size()));
		}else{
			emailDto.setEmailId("CF"+emailDto.getEmailId()+emails.size());
		}
		
		if(gatewayEmailDao.addGateWayEmail(emailDto)){
			return Result.success("发送成功");
		};
		return Result.error("发送失败");
	}

	@Override
	public Result addSMSReSend(String polNo, String phoneNo) throws Exception {
		SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat format1=new SimpleDateFormat("yyyyMMddHHmmssSSS");
		/**查询已发送过的短信*/
		List<GatewaySMS> emss=gatewaySMSDao.querySMSByPolNo(polNo);
		if(emss==null||emss.size()==0){
			return Result.error("此单没有历史短信发送记录");
		}
		GatewaySMS ems=emss.get(0);
		if(ems.getSmsId().contains("CF")){
			ems.setSmsId("CF"+(Integer.parseInt(ems.getSmsId().replace("CF", ""))+emss.size()));
		}else{
			ems.setSmsId("CF"+ems.getSmsId()+emss.size());
		}
		ems.setPhoneCode(phoneNo);
		ems.setSendTime(format.format(new Date()));
		ems.setId("CF"+format1.format(new Date())+"560709");
		if(gatewaySMSDao.addGateWaySMS(ems)){
			return Result.success("发送成功");
		};
		return Result.error("发送失败");
	}

}
